home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / misc / SAPrefsStatic.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  6KB  |  256 lines

  1. /*********************************************************************
  2.  
  3.    SAPrefsStatic
  4.  
  5.    Copyright (C) 2000 Smaller Animals Software, Inc.
  6.  
  7.    This software is provided 'as-is', without any express or implied
  8.    warranty.  In no event will the authors be held liable for any damages
  9.    arising from the use of this software.
  10.  
  11.    Permission is granted to anyone to use this software for any purpose,
  12.    including commercial applications, and to alter it and redistribute it
  13.    freely, subject to the following restrictions:
  14.  
  15.    1. The origin of this software must not be misrepresented; you must not
  16.      claim that you wrote the original software. If you use this software
  17.      in a product, an acknowledgment in the product documentation would be
  18.      appreciated but is not required.
  19.  
  20.    2. Altered source versions must be plainly marked as such, and must not be
  21.      misrepresented as being the original software.
  22.  
  23.    3. This notice may not be removed or altered from any source distribution.
  24.  
  25.    http://www.smalleranimals.com
  26.    smallest@smalleranimals.com
  27.  
  28. **********************************************************************/
  29.  
  30. // SAPrefsStatic.cpp : implementation file
  31. //
  32.  
  33. #include "stdafx.h"
  34. #include "../resource.h"
  35. #include "SAPrefsStatic.h"
  36.  
  37. #if defined(_DEBUG) && !defined(MMGR)
  38. #define new DEBUG_NEW
  39. #undef THIS_FILE
  40. static char THIS_FILE[] = __FILE__;
  41. #endif
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSAPrefsStatic
  45.  
  46. CSAPrefsStatic::CSAPrefsStatic()
  47. {
  48.     m_textClr = ::GetSysColor (COLOR_3DFACE);
  49.     m_fontWeight = FW_NORMAL;
  50.     m_fontSize = 12;
  51. }
  52.  
  53. CSAPrefsStatic::~CSAPrefsStatic()
  54. {
  55.    if (m_bm.GetSafeHandle())
  56.    {
  57.       m_bm.DeleteObject();
  58.    }
  59.  
  60.     if (m_captionFont.GetSafeHandle())
  61.    {
  62.         m_captionFont.DeleteObject();
  63.    }
  64.  
  65.     if (m_nameFont.GetSafeHandle())
  66.    {
  67.         m_nameFont.DeleteObject();
  68.    }
  69. }
  70.  
  71.  
  72. BEGIN_MESSAGE_MAP(CSAPrefsStatic, CStatic)
  73.     //{{AFX_MSG_MAP(CSAPrefsStatic)
  74.     ON_WM_PAINT()
  75.     ON_WM_ERASEBKGND()
  76.    ON_MESSAGE( WM_SETTEXT, OnSetText )
  77.     //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CSAPrefsStatic message handlers
  82.  
  83. void CSAPrefsStatic::OnPaint() 
  84. {
  85.    CPaintDC dc(this); // device context for painting
  86.  
  87.     CFont *pOldFont = NULL;
  88.  
  89.     // Set default font
  90.     if (m_csFontName=="")
  91.     {
  92.         CWnd *pWndParent = GetParent();
  93.         if (pWndParent)
  94.         {
  95.             dc.SelectObject (pWndParent->GetFont());
  96.         }
  97.     }
  98.     else
  99.     {
  100.         // create a font, if we need to
  101.         if (m_captionFont.GetSafeHandle()==NULL)
  102.         {
  103.             m_captionFont.CreateFont( m_fontSize, 
  104.                                                 0, 0, 0, 
  105.                                                 m_fontWeight,
  106.                                                 0, 0, 0, ANSI_CHARSET,
  107.                                                 OUT_DEFAULT_PRECIS,
  108.                                                 CLIP_DEFAULT_PRECIS,
  109.                                                 DEFAULT_QUALITY,
  110.                                                 FF_MODERN,
  111.                                                 m_csFontName);
  112.         }
  113.  
  114.         if (m_captionFont.GetSafeHandle()!=NULL)
  115.             pOldFont = dc.SelectObject(&m_captionFont);
  116.     }
  117.  
  118.     // Draw text
  119.     CString strText;
  120.     GetWindowText(strText);
  121.  
  122.    dc.SetTextColor(m_textClr);
  123.    dc.SetBkMode(TRANSPARENT);
  124.  
  125.     // vertical center
  126.    CRect cr;
  127.    GetClientRect(cr); 
  128.  
  129.     cr.left+=5;
  130.     dc.DrawText( strText, cr, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
  131.  
  132.     if (pOldFont)
  133.         dc.SelectObject(pOldFont);
  134. }
  135.  
  136. BOOL CSAPrefsStatic::OnEraseBkgnd(CDC* pDC) 
  137. {
  138.    if (!m_bm.GetSafeHandle())
  139.    {
  140.       MakeCaptionBitmap();
  141.    }
  142.  
  143.    if (m_bm.GetSafeHandle())
  144.    {
  145.       CRect cr;
  146.       GetClientRect(cr);
  147.  
  148.       CDC memDC;
  149.       memDC.CreateCompatibleDC(pDC);
  150.  
  151.       CBitmap *pOB = memDC.SelectObject(&m_bm);
  152.  
  153.       pDC->BitBlt(0,0,cr.Width(), cr.Height(), &memDC, 0,0, SRCCOPY);
  154.  
  155.       memDC.SelectObject(pOB);
  156.  
  157.    }
  158.  
  159.     return TRUE;
  160. }
  161.  
  162. LRESULT CSAPrefsStatic::OnSetText(WPARAM wParam, LPARAM lParam)
  163. {
  164.     DefWindowProc (WM_SETTEXT, wParam, lParam);
  165.     Invalidate(TRUE);
  166.     return 1;
  167. }
  168.    
  169.  
  170. //////////////////
  171. // Helper to paint rectangle with a color.
  172. //
  173. static void PaintRect(CDC& dc, int x, int y, int w, int h, COLORREF color)
  174. {
  175.     CBrush brush(color);
  176.     CBrush* pOldBrush = dc.SelectObject(&brush);
  177.     dc.PatBlt(x, y, w, h, PATCOPY);
  178.     dc.SelectObject(pOldBrush);
  179. }
  180.  
  181. void CSAPrefsStatic::MakeCaptionBitmap()
  182. {
  183.     if (m_bm.m_hObject)
  184.         return;                                   // already have bitmap; return
  185.  
  186.    CRect cr;
  187.    GetClientRect(cr);
  188.    int w = cr.Width();
  189.    int h = cr.Height();
  190.  
  191.     // Create bitmap same size as caption area and select into memory DC
  192.     //
  193.     CWindowDC dcWin(this);
  194.     CDC dc;
  195.     dc.CreateCompatibleDC(&dcWin);
  196.     m_bm.DeleteObject();
  197.     m_bm.CreateCompatibleBitmap(&dcWin, w, h);
  198.     CBitmap* pOldBitmap = dc.SelectObject(&m_bm);
  199.  
  200.    COLORREF clrBG = ::GetSysColor(COLOR_3DFACE); // background color
  201.     int r = GetRValue(clrBG);                // red..
  202.     int g = GetGValue(clrBG);                // ..green
  203.     int b = GetBValue(clrBG);                // ..blue color vals
  204.     int x = 8*cr.right/8;                    // start 5/6 of the way right
  205.     int w1 = x - cr.left;                    // width of area to shade
  206.  
  207.    int NCOLORSHADES = 128;        // this many shades in gradient
  208.  
  209.     int xDelta= max( w / NCOLORSHADES , 1);    // width of one shade band
  210.  
  211.     PaintRect(dc, x, 0, cr.right-x, h, clrBG);
  212.  
  213.     while (x > xDelta) 
  214.    {                                                // paint bands right to left
  215.         x -= xDelta;                            // next band
  216.         int wmx2 = (w1-x)*(w1-x);            // w minus x squared
  217.         int w2  = w1*w1;                        // w squared
  218.         PaintRect(dc, x, 0, xDelta, h,    
  219.             RGB(r-(r*wmx2)/w2, g-(g*wmx2)/w2, b-(b*wmx2)/w2));
  220.     }
  221.  
  222.     PaintRect(dc,0,0,x,h,RGB(0,0,0));  // whatever's left ==> black
  223.  
  224.     // draw the 'constant' text
  225.  
  226.     // create a font, if we need to
  227.     if (m_nameFont.GetSafeHandle()==NULL)
  228.     {
  229.         m_nameFont.CreateFont( 18, 0, 0, 0, FW_BOLD,
  230.                                             0, 0, 0, ANSI_CHARSET,
  231.                                             OUT_DEFAULT_PRECIS,
  232.                                             CLIP_DEFAULT_PRECIS,
  233.                                             DEFAULT_QUALITY,
  234.                                             FF_MODERN,
  235.                                             m_csFontName);    
  236.     }
  237.  
  238.     CFont * OldFont = dc.SelectObject(&m_nameFont);
  239.  
  240.     // back up a little
  241.     cr.right-=5;
  242.  
  243.     // draw text in DC
  244.     dc.SetBkMode(TRANSPARENT);
  245.     dc.SetTextColor( ::GetSysColor( COLOR_3DHILIGHT));
  246.     dc.DrawText( m_csConstantText, cr + CPoint(1,1), DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
  247.     dc.SetTextColor( ::GetSysColor( COLOR_3DSHADOW));
  248.     dc.DrawText( m_csConstantText, cr, DT_SINGLELINE | DT_RIGHT | DT_VCENTER);
  249.  
  250.     // restore old font
  251.     dc.SelectObject(OldFont);
  252.  
  253.     // Restore DC
  254.     dc.SelectObject(pOldBitmap);
  255. }
  256.